<template>
  <!--  <common-edit-dialog :title="dialogTitle" :visible.sync="dialogShow" width="800px" append-to-body>-->
  <common-edit-dialog :visible="dialogShow" :title="dialogTitle" :button-loading="loading" width="800px"
    @cancel="close('onlyClose')" @submit="submitForm">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" @submit.native.prevent>
      <el-row :gutter="18">
        <el-col :span="12">
          <el-form-item label="应用名称" prop="appId">
            <el-select v-model="form.appId" clearable>
              <el-option v-for="(items, index) in applicationList" :key="items.id" :label="items.applicationName"
                :value="items.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="显示排序" prop="orderNum">
            <common-input-number :max-length="4" v-model="form.orderNum" controls-position="right">
            </common-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <common-input type="textarea" :autosize="{ minRows: 4, maxRows: 8 }" v-model="form.remark"
              placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </common-edit-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { sortRule } from "@/utils/formRules";
import { add, edit } from '@/api/apisecurity/applicationType/applicationType'
import { listLication } from "@/api/apisecurity/application/lication";

export default {
  components: { Treeselect },
  props: {

  },
  data() {
    return {
      dialogTitle: "新增", // 标题
      dialogShow: false, // 是否显示
      form: {
        orgId: null,
        orgName: null,
        orderNum: 1,
        remark: null,
        parentId: null,
        status: 0,
        orgFullName: null,
        ancestors: null,
        appId: null,
        type: 1,
      },
      applicationList: [],
      rules: {
        appId: [
          {
            required: true,
            message: "请选择应用",
            trigger: "blur",
          },
        ],
        orderNum: sortRule,
      },
      loading: false,
    };
  },
  // watch: {
  //   dialogShow(val, oldVal) {
  //     if (!val) {
  //       this.$refs.form.resetFields();
  //       this.$emit("refresh");
  //     }
  //   },
  // },
  computed: {},
  methods: {
    /**
     * 关闭
     */
    close(onlyClose) {
      this.form = this.$options.data().form;
      this.dialogShow = false;
      // if (!onlyClose) {
      //   this.$emit("refresh");
      // }
    },
    /**
     * 显示
     */
    show(data) {
      this.getList()
      this.dialogShow = true;
      if (data.editType === 'edit') {
        this.dialogTitle = "编辑应用"
        if (data && data.orgId) {
          Reflect.ownKeys(this.form).forEach(k => {
            if(k !== 'orgName')this.form[k] = data[k]
          })
        } else {
          this.form = this.$options.data().form;
        }
      } else {
        this.dialogTitle = "新增应用"
        this.form.parentId = data && data.orgId ? data.orgId : ''
      }
      this.form.ancestors = data && data.fullPath ? data.fullPath.join(',') : ''

      // this.form = data && data.orgId ? data : this.$options.data().form;;

      // if(data && data.fullPath) {
      //   this.form.ancestors = data.fullPath.join(',')
      // }
      // if (data.orgId) {
      //   data.orderNum = parseInt(data.orderNum);
      //   this.form = data;
      // } else {
      //   this.form = this.$options.data().form;
      // }
    },
    /**
     * 获取列表数据
     */
    getList() {
      listLication({})
        .then((res) => {
          if (res && res.code === 200) {
            this.applicationList = res.rows
          } else {
            this.applicationList = [];
          }
        })
    },
    /**
     * 提交
     */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (!!this.form.orgId) {
            edit(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("编辑成功");
                  this.close();
                  this.$emit("refresh");
                } else {
                  this.$GlobalErrorConfirm("编辑失败", res.msg).then();
                }
              })
              .finally(() => (this.loading = false));
          } else {
            add(this.form)
              .then((res) => {
                if (res && res.code === 200) {
                  this.$GlobalSuccessMessage("新增成功");
                  this.close();
                  this.$emit("refresh");
                } else {
                  this.$GlobalErrorConfirm("新增失败", res.msg).then();
                }
              })
              .finally(() => (this.loading = false));
          }
        }
      });
    },
  },
  mounted() { },
};
</script>
<style lang="scss" scoped>
</style>
